// 应该快完成了
#include <bits/stdc++.h>

using namespace std;
using LL = long long;
const int N = 1e5 + 10;
vector<LL> hui;
map<LL, LL> mp;

bool check(LL k) {
  string a = to_string(k);
  string b = a;
  reverse(a.begin(), a.end());
  return a == b;
}
void dfs(int step, LL s) {
  if (step >= 15) {
    if (check(s)) {
      hui.push_back(s);
    }
    return;
  }
  dfs(step + 1, s + (1LL << step));
  dfs(step + 1, s);
}

LL work() {
  mp.clear();
  int n;
  cin >> n;
  LL max_hui = 0;
  for (int i = 1; i <= n; i++) {
    LL k;
    cin >> k;
    mp[k]++;
    max_hui = max(max_hui, k);
  }

  LL ans = 0;
  for (const auto &pair : mp) {
    LL x = pair.first;
    LL x_count = pair.second;

    ans += x_count * (x_count - 1) / 2;
    for (int i = 1; i < hui.size(); i++) {
      if (hui[i] > max_hui)
        break;
      LL y = x ^ hui[i];
      LL y_count = mp.count(y);
      if (x < y)
        ans += x_count * y_count;

      //   if (mp.count(y) != 0)
      //     cout << x << " " << value << " " << y << " " << mp.count(y)
      //          << "x^y=" << hui[i] << endl;
    }
  }
  ans += n;
  return ans;
}
int main() {
  dfs(0, 0LL);
  sort(hui.begin(), hui.end());
  //   cout << hui.size();
  //   for (auto x : hui) {
  //     cout << x << " ";
  //   }
  int t;
  cin >> t;
  while (t--) {
    cout << work() << endl;
  }
  return 0;
}
